package com.cazor.dg;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.cazor.common.core.domain.entity.SysDictData;
import com.cazor.dg.domain.DgDevDeviceInstance;
import com.cazor.dg.domain.DgDeviceSensor;
import com.cazor.dg.domain.DgMonitorStation;
import com.cazor.dg.service.IDgDevDeviceInstanceService;
import com.cazor.dg.service.IDgDeviceSensorService;
import com.cazor.dg.service.IDgMonitorStationService;
import com.cazor.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
@EnableTransactionManagement
@Slf4j
public class DgDataUpdate {
    @Resource
    private IDgMonitorStationService monitorStationService;
    @Resource
    private IDgDeviceSensorService deviceSensorService;
    @Resource
    private IDgDevDeviceInstanceService devDeviceInstanceService;
    @Resource
    private ISysDictDataService sysDictDataService;

    @Test
    public void update_dgDevDeviceSensorStationId() {
        List<DgMonitorStation> stationList = monitorStationService.lambdaQuery()
                .eq(DgMonitorStation::getStatus, 1)
                .eq(DgMonitorStation::getValid, "1").list();

        int stationCount = CollectionUtil.size(stationList);
        if (stationCount > 0) {
            for (DgMonitorStation monitorStation : stationList) {
                deviceSensorService.lambdaUpdate()
                        .set(DgDeviceSensor::getStationId, monitorStation.getStationId())
                        .eq(DgDeviceSensor::getStationName, monitorStation.getName())
                        .update();
            }
        }
    }

    /**
     * 更新 传感器 数据表中的 监测内容id
     */
    @Test
    public void update_dgDeviceSensor_ContentId() {
        List<DgMonitorStation> stationList = monitorStationService.lambdaQuery()
                .eq(DgMonitorStation::getStatus, 1)
                .eq(DgMonitorStation::getValid, "1").list();

        int stationCount = CollectionUtil.size(stationList);
        if (stationCount > 0) {

            List<DgDevDeviceInstance> instanceList = null;
            List<DgDeviceSensor> sensorList = null, updateSensorList = new ArrayList<>();
            DgDeviceSensor updateObj = null;
            int sensorCount = 0;
            for (DgMonitorStation monitorStation : stationList) {
                instanceList = devDeviceInstanceService.lambdaQuery()
                        .eq(DgDevDeviceInstance::getStationId, monitorStation.getStationId())
                        .list();
                int instanceCount = CollectionUtil.size(instanceList);
                if (instanceCount > 0) {
                    for (DgDevDeviceInstance devDeviceInstance : instanceList) {
//                        if (devDeviceInstance.getContentName().contains("基站")){
//                            continue;
//                        }
                        sensorList = deviceSensorService.lambdaQuery()
                                .eq(DgDeviceSensor::getStationId, monitorStation.getStationId())
                                .likeRight(DgDeviceSensor::getName, devDeviceInstance.getDeviceId()).list();
                        sensorCount += CollectionUtil.size(sensorList);
                        if (CollectionUtil.isNotEmpty(sensorList)){
                            for (DgDeviceSensor sensor : sensorList) {
                                updateObj = new DgDeviceSensor();
                                updateObj.setId(sensor.getId());
                                updateObj.setContentId(devDeviceInstance.getContentId());
                                updateSensorList.add(updateObj);
                            }
//                            Console.log("{}--{}",
//                                    monitorStation.getStationId(), devDeviceInstance.getDeviceId());
                        }
//                        Console.log("{}--{}--{}",
//                                monitorStation.getStationId(), devDeviceInstance.getDeviceId(),
//                                sensorList.stream().map(DgDeviceSensor::getName).collect(Collectors.toList()));
                    }
                }
            }
            // 批量更新 contentId
            if (CollectionUtil.isNotEmpty(updateSensorList)) {
                int updateSensorCount = CollectionUtil.size(updateSensorList);
                log.info("updateSensorCount:{}; sensorCount:{}", updateSensorCount, sensorCount);

                boolean f = deviceSensorService.updateBatchById(updateSensorList);
                log.info("批量更新完成:{}", f);

            }
        }

    }

    @Test
    public void insert_dict_monitorObjectType(){
        String str = "1,滑坡;2,不稳定斜坡;3,崩塌;4,泥石流;5,采空区塌陷;6,岩溶塌陷;7,坡面水毁;8,台田地水毁;9,河沟道水毁;10,泥石流;11,黄土湿陷;12,地裂缝;13,盐渍土;14,冻帐融沉;15,地面沉降;16,风蚀沙埋;30,区域降雨;31,地震断裂带;50,环焊缝;51,穿跨越;70,高后果区;71,振动影响;72,隧道管道保护;73,高填方;80,天然气泄漏";

        this.insertSysDictData(str, "dg_monitor_object_type");

    }

    @Test
    public void inset_dict_contentType(){
        String str = "1,地下水位;2,墒情;3,含盐率;4,温度;5,流量;6,孔隙水压;7,激光泥位;8,深部位移;9,地表位移;10,多点位移;11,地表裂缝;12,土压力;13,沉降;14,雨量;15,管道应变;16,管道位移;17,钢筋记;18,抗滑桩内力;19,甲烷;20,分布式光纤;21,流速;22,水深;23,拉线位移;24,倾角;25,雷达泥位";

        this.insertSysDictData(str, "dg_content_type");

    }

    private void insertSysDictData(String dataStr, String dictType) {
        List<String> list = StrUtil.split(dataStr, ";");

        List<String> l2 = null;
        long i = 1L;
        for (String s : list) {
            l2 = StrUtil.split(s, ",");
            if (CollectionUtil.size(l2) == 2) {
                SysDictData data = new SysDictData();
                data.setDictSort(i++);
                data.setDictLabel(l2.get(1));
                data.setDictValue(l2.get(0));
                data.setDictType(dictType);
                data.setCssClass("");
                data.setListClass("primary");
                data.setIsDefault("N");
                data.setStatus("0");
                data.setCreateBy("admin");
                data.setCreateTime(new Date());
                sysDictDataService.insertDictData(data);
            } else {
                log.warn("错误的字典数据:{}", s);
            }
        }
    }

}
